from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.shortcuts import redirect
from rest_framework import permissions
from rest_framework_simplejwt.views import (
    TokenObtainPairView,
    TokenRefreshView,
)
from users import views as users_views  # 导入用户视图

def redirect_to_admin(request):
    return redirect('admin:index')

urlpatterns = [
    path('', redirect_to_admin),  # 添加根URL重定向到管理后台
    path("admin/", admin.site.urls),
    
    # JWT认证 - 使用SimpleJWT提供的视图
    path("api/token/", TokenObtainPairView.as_view(), name="token_obtain_pair"),
    path("api/token/refresh/", TokenRefreshView.as_view(), name="token_refresh"),
    
    # 自定义认证API（按照API文档规范）
    path("api/auth/register/", users_views.register, name="auth_register"),
    path("api/auth/login/", users_views.login, name="auth_login"),
    path("api/auth/login-by-phone/", users_views.login_by_phone, name="auth_login_by_phone"),
    path("api/auth/refresh/", users_views.refresh_token, name="auth_token_refresh"),
    path("api/auth/logout/", users_views.logout, name="auth_logout"),
    path("api/auth/guest-login/", users_views.guest_login, name="auth_guest_login"),
    
    # 各应用API路由
    path("api/users/", include("users.urls")),
    path("api/assets/", include("assets.urls")),
    path("api/services/", include("services.urls")),
    path("api/finance/", include("finance.urls")),
    path("api/notifications/", include("notification.urls")),
    path("api/core/", include("core.urls")),
    path('api/analytics/', include('analytics.urls')),
]

# 开发环境下添加媒体文件的URL
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
